package com.aaa.service.impl;

import com.aaa.dao.T_CyglDao;
import com.aaa.service.T_CyglService;
import com.aaa.util.JavaMail;
import com.aaa.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class T_CyglServiceImpl implements T_CyglService {
    @Autowired
    private T_CyglDao cyglDao;

    @Override
    public List<Map> selectAll(Map map) {
        return cyglDao.selectAll(map);
    }

    @Override
    public Integer getCount(Map map) {
        return cyglDao.getCount(map);
    }

    @Override
    public Integer del(Integer id) {
        return cyglDao.del(id);
    }

    @Override
    public List<Map> infoList() {
        return cyglDao.infoList();
    }

    @Override
    public Integer upd(Map map) {
        return cyglDao.upd(map);
    }

    @Override
    public int addEaxmPerson(Map map) {
        String email = (String) map.get("email");
        //随机生成账号和密码
        String str = "";
        String username="";
        String password="";
        int a=0;
        Integer total= (Integer) map.get("total");//公司人数
        Integer peopel= (Integer) map.get("peopel");//考试比例
        double number=total*(peopel*0.01);
        for (int i=1;i<=number;i++) {
            username = PasswordUtil.randomString(6);
            password = PasswordUtil.randomString(6);
            map.put("username", username);
            map.put("password", password);
            //给注册单位添加考试信息
            cyglDao.addExamPerson(map);
            a++;
            str+="账号:"+username+"; "+"密码:"+password+"<br/>";
        }
        JavaMail se = new JavaMail(false);
        se.doSendHtmlEmail("考试账号", str,email);
        //改状态
        cyglDao.updstate(map);
        return a;
    }

    @Override
    public Map max() {
        Map map=new HashMap();
        for (int i=1;i<=7;i++){
            String max="max"+i;
            map.putAll(cyglDao.max1(max,i));
        }

        return map;
    }

    @Override
    public int add(Map map) {
        int b=cyglDao.add(map);

        for (int i=1;i<=7;i++){
            String count="count"+i;
            Integer a= (Integer) map.get(count);
            List<Map> list=cyglDao.selQuestion(i,a);
            for (int j=0;j<list.size();j++) {
                Map map1=list.get(j);
                map1.putAll(cyglDao.selExam_id());
                cyglDao.addQuestion(map1);
            }

        }
        return b;
    }

    /**
     * 根据时间更新考试状态
     */
    @Override
    public void timing() {
        cyglDao.begin();
        cyglDao.end();
    }
}
